﻿using com.nm.ApiServer;
using com.nm.ApiServer.Attributes;
using com.nm.Attributes;
using com.nm.db.Models.Systems;
using com.nm.library;
using com.nm.library.Helper;
using george.Data;
using InventoryToPredictAPI.model;
using NHibernateVertica;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntityFramework.Extensions;

namespace InventoryToPredictAPI
{
    [MustLogin]
    public class UserCategoryManagerHandler : BaseHandler
    {
        public VerticaDataHelper Helper { get; set; } = new VerticaDataHelper();
        #region 查询用户列表
        public ApiResult GetUserList(string UserName = "")
        {
            try
            {
                var dc = new SysEntities();
                var query = (from m in dc.sys_users
                             select m);
                //不为空查询
                if (!string.IsNullOrEmpty(UserName))
                {
                    query=query.Where(m => m.UserName.Contains(UserName) || m.UserNo.Contains(UserName));
                }
                var result = query.OrderBy(q => q.UserID).Select(
                    q => new UserCategoryManagerModel.UserList
                    {
                        UserID = q.UserID,
                        UserName = q.UserName,
                        UserNo = q.UserNo
                    }).ToList();
                return Success(result);
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }

        #endregion

        #region 获取类别列表
        /// <summary>
        /// 获取类别列表
        /// </summary>
        /// <returns></returns>
        public ApiResult GetCategoryList()
        {
            try
            {
                List<UserCategoryManagerModel.CategoryList> list = new List<UserCategoryManagerModel.CategoryList>();
                string sql = @" select ExtrSource,KindID,KindCode,KindName from nods.nods_erp_SD_Mat_Kind  where ExtrSource='NM' AND AllowUsed=1 order by KindID asc ";
                var cmd = Helper.GetSqlStringCommand(sql);
                var dt = Helper.ExecuteDataTable(cmd);
                list = ModelConvertHelper<UserCategoryManagerModel.CategoryList>.ConvertToModel(dt).ToList();
                return Success(list);
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }

        #endregion
        /// <summary>
        /// 查询用户类别权限
        /// </summary>
        /// <param name="UserID">用户ID</param>
        /// <returns></returns>
        public ApiResult GetUserCategoryManagerList(string UserID = "")
        {
            if (string.IsNullOrWhiteSpace(UserID) || UserID.ToUpper() == "UNDEFINED")
            {
                return null;
            }
            try
            {
                int User_ID = Convert.ToInt32(UserID);
                var dc = new SysEntities();
                var query = (from m in dc.UserCategory_Jurisdiction
                             where m.UserID == User_ID && m.IsSelect == true
                             select m);

                var result = query.OrderBy(q => q.UserID).Select(
                    q => new UserCategoryManagerModel.UserCategoryManagerList
                    {
                        UserID = q.UserID,
                        KindID = q.KindID,
                        IsSelect = q.IsSelect,
                        ParentID = q.ParentJurisdictionID
                    }).ToList();
                return Success(result);
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }

        }

        [JsonType]
        [MustLogin]
        [Description("保存数据")]
        [ReturnType(typeof(int), "0-成功")]
        public ApiResult SaveCategoryManager([ParamDescription("修改数据")]UserCategoryManagerModel.ModifyData CategoryList)
        // public ApiResult SaveCategoryManager(int UserID, string[] CategoryList)
        {

            if (CategoryList == null) return Error("参数对象不能为空！");
            var dc = new SysEntities();
            var tran = dc.Database.BeginTransaction();
            try
            {
                dc.UserCategory_Jurisdiction.Where(t => t.UserID == CategoryList.UserID).Delete();

                if (CategoryList != null)
                {
                    foreach (var item in CategoryList.CategoryList)
                    {
                        var Category = new UserCategory_Jurisdiction
                        {
                            UserID = CategoryList.UserID,
                            KindID = item,
                            CompanyID = UserInfo.UserCompanyId,
                            ExtrSource = UserInfo.UserExtrSource,
                            IsSelect = true
                            //ParentJurisdictionID
                        };
                        dc.UserCategory_Jurisdiction.Add(Category);
                    }
                }
                dc.SaveChanges();
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                return Error(ex.Message);
            }
            return Success(0);

        }


    }
}
